package com.eatme.leetcode;

public class ListNode {
        public int val;
        public ListNode next;
        ListNode() {}
        public ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }

        // 链表节点构造函数
        // 使用arr为参数，创造一个链表，当前ListNode为链表节点
        public ListNode(int[] arr) {
            if (arr==null ||arr.length==0)
                throw new IllegalArgumentException("arr can not be empty");
            this.val = arr[0];
            ListNode cur = this;
            for (int i = 1; i < arr.length; i++) {
                cur.next =new ListNode(arr[i]);
                cur =cur.next;
            }
        }

        // 以当前节点为头节点的链表信息字符串
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            ListNode cur = this;
            while (cur!=null) {
                sb.append(cur.val+"->");
                cur =cur.next;
            }
            sb.append("NULL");
            return sb.toString();
        }
    }